package com.hq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.bean.Student;
import com.hq.bean.Users;
import com.hq.service.StudentService;
import com.hq.util.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Resource
    private StudentService service;

    @RequestMapping("list.do")
    public String list(Model model, @RequestParam(value = "pages",defaultValue = "1")Integer pages, String sname, String sex, String start, String end, String sheng, String shi, String xian){
        HashMap map = new HashMap();
        map.put("sname",sname);
        map.put("sex",sex);
        map.put("start",start);
        map.put("end",end);
        map.put("sheng",sheng);
        map.put("shi",shi);
        map.put("xian",xian);
        PageHelper.startPage(pages,3);
        List<Student> list = service.list(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }

    @RequestMapping("teacher.do")
    @ResponseBody
    public Object teacher(){
        return service.teacher();
    }

    @RequestMapping("city.do")
    @ResponseBody
    public Object city(Integer pid){
        return service.city(pid);
    }

    @RequestMapping("login.do")
    public String login(Users users, HttpSession session){
        Users login = service.login(users);
        if (login!=null){
            session.setAttribute("user",login);
            return "redirect:list.do";
        }else {
            return "redirect:index.jsp";
        }
    }

    @RequestMapping("toregister.do")
    public String toregister(){
        return "register";
    }

    @RequestMapping("addRegister.do")
    public String addRegister(Users users){
        int i = service.addRegister(users);
        return "redirect:index.jsp";
    }

    @RequestMapping("toadd.do")
    public String toadd(){
        return "add";
    }

    @RequestMapping("add")
    @ResponseBody
    public Object add(Student student, Integer[] tids, HttpSession session, MultipartFile[] photo){
        String[] uploads = FileUploadUtil.loads(session, photo, "upload");
        student.setPic1(uploads[1]);
        student.setPic2(uploads[2]);
        student.setPic3(uploads[3]);
        student.setPic4(uploads[4]);
        student.setPic5(uploads[5]);
        Users user = (Users) session.getAttribute("user");
        student.setUid(user.getUid());
        int i= service.add(student,tids);
        return i;
    }

    @RequestMapping("getBySid")
    @ResponseBody
    public Object getBySid(Integer sid){
        Map map=service.getBySid(sid);
        return map;
    }

    @RequestMapping("toupd.do")
    public String toupd(){
        return "upd";
    }


    @RequestMapping("upd.do")
    @ResponseBody
    public Object upd(Student student, Integer[] tids, HttpSession session, MultipartFile[] photo){
        String[] uploads = FileUploadUtil.loads(session, photo, "upload");
        student.setPic1(uploads[1]);
        student.setPic2(uploads[2]);
        student.setPic3(uploads[3]);
        student.setPic4(uploads[4]);
        student.setPic5(uploads[5]);
        Users user = (Users) session.getAttribute("user");
        student.setUid(user.getUid());
        int i= service.upd(student,tids);
        return i;
    }

    @RequestMapping("del.do")
    @ResponseBody
    public Object del(Integer sid){
        int i=service.del(sid);
        return i;
    }

    @RequestMapping("yz.do")
    @ResponseBody
    public Object yz(String uname){
        int i= service.yz(uname);
        return i;
    }

    @RequestMapping("dels.do")
    @ResponseBody
    public Object dels(String sid){
        int i=service.dels(sid);
        return i;
    }
}
